package com.chushouya.product.rabbitmq.listener;

import com.chushouya.common.dto.QueueMessage;
import com.chushouya.product.service.api.ResolveEvaluateApiService;
import com.general.framework.core.exception.BaseException;
import com.general.framework.core.exception.ViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 商品评估解析消息监听器
 * 监听商品评估解析消息，调用ResolveEvaluateApiService.resolveEvaluateItem方法
 * 
 * @author chushouya
 */
@Component
@Slf4j
public class ResolveEvaluateMessageListener {

    @Resource
    private ResolveEvaluateApiService resolveEvaluateApiService;

    /**
     * 监听商品评估解析消息
     */
    @RabbitListener(queuesToDeclare = @Queue(name = "chushouya.product.evaluate.resolve", durable = "true"))
    public void handleResolveEvaluateMessage(QueueMessage<?> queueMessage) {
        log.info("收到商品评估解析消息: messageId={}, messageType={}, data={}", 
                queueMessage.getMessageId(), queueMessage.getMessageType(), queueMessage.getData());
        
        try {
            // 验证消息数据
            if (queueMessage.getData() == null) {
                log.error("商品评估解析消息数据为空: messageId={}", queueMessage.getMessageId());
                log.warn("数据为空的消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
                return;
            }
            
            // 获取商品ID
            Long productId = extractProductId(queueMessage.getData());
            if (productId == null) {
                log.error("无法从消息中提取商品ID: messageId={}, data={}", 
                        queueMessage.getMessageId(), queueMessage.getData());
                log.warn("无效商品ID消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
                return;
            }
            
            // 验证商品ID有效性
            if (productId <= 0) {
                log.error("商品ID无效: productId={}, messageId={}", productId, queueMessage.getMessageId());
                log.warn("无效商品ID消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
                return;
            }
            
            log.info("开始处理商品评估解析，商品ID: {}, messageId: {}, messageType: {}", 
                    productId, queueMessage.getMessageId(), queueMessage.getMessageType());
            
            // 调用商品评估解析服务
            resolveEvaluateApiService.resolveEvaluateItem(productId);
            
            log.info("商品评估解析处理完成，商品ID: {}, messageId: {}", productId, queueMessage.getMessageId());
            
        } catch (ViolationException e) {
            log.error("商品评估解析消息验证错误: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
            // 验证异常不抛出，避免消息重新入队
            log.warn("验证异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        } catch (BaseException e) {
            log.error("商品评估解析消息业务异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
            // 业务异常不抛出，避免消息重新入队
            log.warn("业务异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        } catch (Exception e) {
            log.error("处理商品评估解析消息异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId(), e);
            // 其他异常不抛出，避免消息重新入队
            log.warn("异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        }
    }

    /**
     * 从消息数据中提取商品ID
     * 
     * @param data 消息数据
     * @return 商品ID
     */
    private Long extractProductId(Object data) {
        try {
            if (data instanceof Long) {
                return (Long) data;
            } else if (data instanceof Integer) {
                return ((Integer) data).longValue();
            } else if (data instanceof String) {
                return Long.valueOf(data.toString());
            } else {
                log.error("商品ID数据类型不支持: dataType={}, value={}", 
                        data.getClass().getSimpleName(), data);
                return null;
            }
        } catch (NumberFormatException e) {
            log.error("商品ID格式转换失败: data={}, error={}", data, e.getMessage());
            return null;
        }
    }
}
